<?php

namespace App\Http\Controllers\Admin;

use App\Models\Member;
use App\Models\MemberAudit;
use App\Services\UmengService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OwnerController extends Controller
{
    public function index()
    {
        return view('admin.owner.index');
    }

    public function data(Request $request)
    {
        $field = [
            'v_members.*',
            'au.audit_status',
            'd.status as verify_status',
            'd.card_no',
            'd.updated_at as uptime',
        ];
        $model = Member::query()
            ->leftJoin('v_member_audit as au', 'au.member_id', '=', 'v_members.id')
            ->leftJoin('v_owners as d', 'd.member_id', '=', 'v_members.id')
            ->select($field)
            ->where('v_members.role_id', Member::ROLE_OWNER);

        if ($request->get('name')) {
            $model = $model->where('v_members.name', 'like', '%' . $request->get('name') . '%');
        }
        if ($request->get('phone')) {
            $model = $model->where('v_members.phone', 'like', '%' . $request->get('phone') . '%');
        }
        if ($request->get('card_no')) {
            $model = $model->where('d.card_no', 'like', '%' . $request->get('card_no') . '%');
        }
        if ($request->get('verify_status', '') != '') {
            $model = $model->where('d.status', $request->get('verify_status'));
        }
        if ($request->get('audit_status')) {
            $model = $model->where('au.audit_status', $request->get('audit_status'));
        }

        $res = $model->orderBy('created_at', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();

        return $this->table($res['data'], $res['total']);
    }

    /**
     * 查看
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        $member = Member::query()
            ->with(['owner', 'audit'])
            ->where('v_members.id', $id)
            ->firstOrFail();

        return view('admin.owner.show', ['member' => $member]);
    }

    /**
     * 审核
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function verify($id)
    {
        $member = Member::query()
            ->with(['owner', 'audit'])
            ->where('v_members.id', $id)
            ->firstOrFail();

        return view('admin.owner.verify', ['member' => $member]);
    }

    /**
     * 更新审核状态
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $member = Member::findOrFail($id);

        $data   = $request->only(['audit_des', 'audit_status']);

        if (MemberAudit::where('member_id', $id)->update($data)) {


            // 当审核成功或失败时，发送通知给用户 - 只审核司机或货主
            if (!in_array($member->role_id, [Member::ROLE_DRIVER, Member::ROLE_OWNER])) {
                return redirect()->to(route('admin.owner'))->with(['status' => '审核完成']);
            }

            $pushType = $member->role_id == Member::ROLE_DRIVER ? 'driver' : 'owner';
            $message  = $data['audit_status'] == MemberAudit::STATUS_PASS ? '恭喜您已经通过审核 ' : sprintf('抱歉因为资料问题没有通过审核，请编辑再次提交；失败原因：%s', $data['audit_des']);
            if ($member->user_platform == Member::USER_PLATFORM_WXXCX) {
//                Sms::send($member->phone, new AuditMessage(compact('message')));
                return redirect()->to(route('admin.owner'))->with(['status' => '审核完成']);
            }

            $paramsBuilder = [];
            switch ($member->user_platform) {
                case Member::USER_PLATFORM_ANDROID:
                    $paramsBuilder = [
                        'alias_type'  => 'user_alias',
                        'alias'       => $member->uuid,
                        "ticker"      => "审核结果通知",
                        "title"       => "审核结果通知",
                        "text"        => $message,
                        "after_open"  => "go_app",
                        "description" => "审核结果通知",
                    ];
                    break;
                case Member::USER_PLATFORM_IOS:
                    $paramsBuilder = [
                        'alert'       => [
                            'title' => '审核结果通知',
                            'body'  => $message,
                        ],  //字符串或者JSON
                        'alias_type'  => 'user_alias',
                        'alias'       => $member->uuid,
                        "description" => "审核结果通知"
                    ];
                    break;
            }

            $umengService = (new UmengService($pushType))->paramsBuilder($paramsBuilder);
            $umengService->easyCustomizedCast($member);


            return redirect()->to(route('admin.owner'))->with(['status' => '审核完成']);
        }
        return redirect()->to(route('admin.owner.verify'))->withErrors('系统错误');
    }

}
